<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MedicalDiagnose extends Model
{
    protected $table = 'st_medical_diagnose';

    protected $fillable = [
        'record_id', 'diagnose', 'remark'//, 'cndiagnoses', 'cn_diagnose',
    ];

    protected $casts = [
        'cn_diagnose' => 'array',
//        'cndiagnoses' => 'array',
    ];


    protected $hidden = ['cn_diagnose', 'cndiagnoses'];

    public function medicalRecord()
    {
        return $this->belongsTo(MedicalRecord::class, 'record_id');
    }

    public function cnDiagnoses()
    {
        return $this->hasMany(CnDiagnose::class, 'record_id', 'record_id')->with('disease');
    }

    //  修改器
    public function setDiagnoseAttribute($value)
    {
        $this->attributes['diagnose'] = is_array($value) ? implode(',', $value) : $value;
    }

    //  获取器
    public function getDiagnoseAttribute($value)
    {
        return DiseaseDictionary::select(['id', 'name', 'icd'])
                                ->whereIn('id', explode(',', $value))->get();
    }
}
